﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WebMobie.Models.Entities;
using WebMobie.Models;
using WebMobie.Modules.Role;

namespace WebMobie.Controllers
{
    public class UserController : ApiController
    {
        private WebMobileContext db = new WebMobileContext();

        // GET api/User
	//[Authorize(Roles = "Admin")]
        public IEnumerable<User> GetUsers()
        {
            return db.Users.AsEnumerable();
        }

        // GET api/User/5
	//[Authorize(Roles = "Admin")]
		[Authorize(Roles = "Admin")]
        public User GetUser(int id)
        {
            User user = db.Users.Find(id);
            if (user == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return user;
        }

        // PUT api/User/5
		[Authorize(Roles = "Admin")]
        public HttpResponseMessage PutUser(User user)
        {
            if (ModelState.IsValid)
            {
		 // check modify Role
		    User prevUser = db.Users.Find(user.UserID);
		    var roleManage = new RoleManager();
		    try
		    {
			    //admin
			    roleManage.VerifyAddRemoveRole(user.LogName, RoleName.Admin, prevUser.IsAccessToAdmin, user.IsAccessToAdmin);
			    //sales
			    roleManage.VerifyAddRemoveRole(user.LogName, RoleName.SalesMan, prevUser.IsEmployee, user.IsEmployee);
		    }
		    catch (Exception ex)
		    {

		    }
		    db.Entry(prevUser) .State = EntityState.Detached;
		    db.Entry(user).State = EntityState.Modified;
                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

		return Request.CreateResponse(HttpStatusCode.OK, user);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/User
        public HttpResponseMessage PostUser(User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, user);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = user.UserID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/User/5
        public HttpResponseMessage DeleteUser(int id)
        {
            User user = db.Users.Find(id);
            if (user == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.Users.Remove(user);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, user);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

	public IEnumerable<User> GetShipers()
	{
		return (from user in db.Users where user.IsBlocked == false select user).ToArray();
	}
    }
}